package com.ruoyi.ucar.util;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class SmsUtil {

    @Value("${ucar.sms.alibaba.endpoint}")
    private String endpoint;

    @Value("${ucar.sms.alibaba.accessKeyId}")
    private String accessKeyId;

    @Value("${ucar.sms.alibaba.accessKeySecret}")
    private String accessKeySecret;

    @Value("${ucar.sms.alibaba.signature}")
    private String signName;

    // 初始化客户端（需替换实际AK和区域）
    public   Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = endpoint;
        return new Client(config);
    }

    // 发送短信验证码
    public   SendSmsResponse sendVerificationCode(
            String phone,
            String code,
            String templateCode
    ) throws Exception {
        Client client = createClient(
        );
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\":\"" + code + "\"}");

        RuntimeOptions runtime = new RuntimeOptions();
        return client.sendSmsWithOptions(request, runtime);
    }

}
